package com.softaria.server.seo;

import java.util.Iterator;
import java.util.List;

import com.softaria.windows.config.MenuConfiguration;
import com.softaria.windows.model.ModelUrl;
import com.softaria.windows.model.PerspectiveUrl;
import com.softaria.windows.model.ViewUrl;
import com.softaria.windows.seo.ParameterSet;
import com.softaria.windows.seo.ModelUrlBuilder;

public class StaticMenu {

  private MenuConfiguration menu;

  public StaticMenu(MenuConfiguration menu) {
    super();
    this.menu = menu;
  }

  private String makeLink(String prefix, ModelUrl perspective) {
    ModelUrlBuilder urlBuilder = StaticPagesFactory.getInstance().getUrlBuilder();

    ParameterSet params = urlBuilder.extractUrlParameters(perspective);

    StringBuffer parameters = new StringBuffer();

    if (prefix != null) {
      prefix = prefix.trim();
      if (prefix.endsWith("/")) {
        prefix = prefix.substring(0, prefix.length() - 1);
      }
    }

    if (!params.getParameterNames().isEmpty()) {
      parameters.append('?');
    }

    for (Iterator<String> it = params.getParameterNames().iterator(); it.hasNext();) {
      String p = it.next();
      parameters.append(p).append('=').append(params.getParameter(p));
      if (it.hasNext()) {
        parameters.append('&');
      }

    }

    return "<a href=\"" + prefix + parameters + "\">" + menu.getMenuLabel(perspective) + "</a>";
  }

  private String makeDefaultLink(String prefix) {
    return "<a href=\"" + prefix + "\">...</a>";
  }

  public String getMenuFooter(String urlPrefix, ModelUrl url) {

    StringBuffer sb = new StringBuffer();

    sb.append("<hr/>");

    sb.append(makeDefaultLink(urlPrefix));

    for (PerspectiveUrl perspective : menu.getPerspecives()) {
      if (perspective.equals(url)) {
        continue;
      }

      sb.append(" | ");
      sb.append(makeLink(urlPrefix, perspective));

    }

    if (url.isPerspective()) {
      // Add views of perspective:
      List<ViewUrl> internalViews = menu.getViews((PerspectiveUrl) url);

      if (internalViews != null) {
        for (ViewUrl vurl : internalViews) {
          sb.append(" | ");
          sb.append(makeLink(urlPrefix, vurl));
        }
      }
    }

    return sb.toString();
  }

  public String getMenuPage(String urlPrefix) {
    StringBuffer sb = new StringBuffer();

    for (PerspectiveUrl perspective : menu.getPerspecives()) {
      sb.append(makeLink(urlPrefix, perspective));
      sb.append("<br/>");
      for (ViewUrl view : menu.getViews(perspective)) {
        sb.append("&nbsp;&nbsp;&nbsp;");
        sb.append(makeLink(urlPrefix, view));
        sb.append("<br/>");
      }
    }

    return sb.toString();

  }

  // public String getSitemap(String urlPrefix) {
  // StringBuffer ret = new StringBuffer();
  //
  // ret.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
  // ret.append("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">")
  // ;
  //
  // for (PerspectiveUrl perspective : menu.getPerspecives()) {
  // ret.append(makeUrlXml(urlPrefix, perspective));
  // for (ViewUrl view : menu.getViews(perspective)) {
  // ret.append(makeUrlXml(urlPrefix, view));
  // }
  // }
  //
  // ret.append("</urlset>");
  //
  // return ret.toString();
  // }
  //
  // private String makeUrlXml(String urlPrefix, PerspectiveUrl url) {
  // StringBuffer ret = new StringBuffer();
  // ret.append("<url>");
  // ret.append("<loc>");
  // ret.append(makeLink(urlPrefix, url));
  // ret.append("</loc>");
  // ret.append("<lastmod>");
  // // 2005-01-01
  // ret.append("</lastmod>");
  // // <changefreq>weekly</changefreq>
  //
  // // <priority>0.8</priority>
  //
  // ret.append("</url>");
  //
  // return ret.toString();
  // }

  public MenuConfiguration getMenuConfiguration() {

    return menu;
  }

}
